/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
// TODO ：【锁屏】可能后面删除掉
import dayjs from 'dayjs'

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
const DATE_FORMAT = 'YYYY-MM-DD'

export function formatToDateTime(date?: dayjs.ConfigType, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format)
}

export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string {
  return dayjs(date).format(format)
}

export const dateUtil = dayjs

// 转换时间
export const dayjsFormat = (value: any, formatStr = 'YYYY-MM-DD') => {
  let str = ''
  if (!!value || value === 0) {
    str = dayjs(value).format(formatStr || 'YYYY-MM-DD')
  }
  return str
}

function getTimeRange(rangeType: any) {
  const start = new Date()
  const end = new Date()

  switch (rangeType) {
    // 今日
    case 'today':
      start.setHours(0, 0, 0, 0)
      end.setHours(23, 59, 59, 999)
      break
    // 昨日
    case 'yesterday':
      start.setDate(start.getDate() - 1)
      start.setHours(0, 0, 0, 0)
      end.setDate(end.getDate() - 1)
      end.setHours(23, 59, 59, 999)
      break
    // 最近七天
    case 'last7days':
      start.setDate(start.getDate() - 6)
      start.setHours(0, 0, 0, 0)
      end.setHours(23, 59, 59, 999)
      break
    // 这个月
    case 'currentMonth':
      start.setDate(1)
      start.setHours(0, 0, 0, 0)
      end.setMonth(end.getMonth() + 1)
      end.setDate(0)
      end.setHours(23, 59, 59, 999)
      break
    // 上个月
    case 'lastMonth':
      start.setMonth(start.getMonth() - 1)
      start.setDate(1)
      start.setHours(0, 0, 0, 0)
      end.setDate(0)
      end.setHours(23, 59, 59, 999)
      break
    // 最近3个月
    case 'before3Month':
      start.setMonth(start.getMonth() - 2)
      start.setDate(1)
      start.setHours(0, 0, 0, 0)
      end.setMonth(end.getMonth() + 1)
      end.setDate(0)
      end.setHours(23, 59, 59, 999)
      break
    default:
      console.error('Invalid range type')
      return
  }
  return [start, end]
}
export { getTimeRange }

/**
 * datetime pick 配置
 */
export const datepick_shortcuts = [
  {
    text: '今日',
    value: () => getTimeRange('today')
  },
  {
    text: '昨日',
    value: () => getTimeRange('yesterday')
  },
  {
    text: '最近七日',
    value: () => getTimeRange('last7days')
  },
  {
    text: '这个月',
    value: () => getTimeRange('currentMonth')
  },
  {
    text: '上个月',
    value: () => getTimeRange('lastMonth')
  },
  {
    text: '近3个月',
    value: () => getTimeRange('before3Month')
  }
]
